package org.realogic.btp.dhtmlx.dao;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.realogic.btp.dhtmlx.define.DhtmlxGridField;
import org.realogic.cfm.KSystemEnvs;
import org.realogic.cfm.util.UtilXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

public class DhtmlxGridRefDefineDAO {
	
	private String filePathName;
	private List<DhtmlxGridField> items;
	private String prex;

	public DhtmlxGridRefDefineDAO(String theFileName, String thePrex) {
		this.filePathName = KSystemEnvs.getInstance().getDhtmlxGridPath() + "/ref/" + theFileName + ".xml";
		this.prex = thePrex;
	}

	private String getPrex_() {
		if (prex == null || "".equals(prex.trim())) {
			return "";
		} else {
			return prex.trim() + ".";
		}
	}

	private Element getRoot() {
		Element root = null;
		try {
			URL url = new URL("file:///" + filePathName);
			root = loadDocument(url);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return root;
	}

	private Element loadDocument(URL url) {
		Document doc = null;
		try {
			InputSource xmlInp = new InputSource(url.openStream());
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
			doc = parser.parse(xmlInp);
			Element root = doc.getDocumentElement();
			root.normalize();
			return root;
		} catch (Exception pce) {
			System.err.println("DhtmlxGridRefDefineDAO error: " + pce);
		}
		return null;
	}

	public List<DhtmlxGridField> doParse() {
		Element tmpRoot = getRoot();
		if (tmpRoot != null) {
			load(tmpRoot);
		}
		return items;
	}

	private void load(Element element) {

		List<Element> flagElementList = UtilXml.childElementList(element, "column");
		items = new ArrayList<DhtmlxGridField>((flagElementList.size() * 4) / 3 + 1);

		Element flagElement = null;
		DhtmlxGridField dhtmlxGridHeader = null;
		if (flagElementList != null) {
			Iterator<Element> flagElementIt = flagElementList.iterator();
			while (flagElementIt.hasNext()) {
				dhtmlxGridHeader = new DhtmlxGridField();
				flagElement = flagElementIt.next();
				dhtmlxGridHeader.setDescription(flagElement.getTextContent().trim());
				dhtmlxGridHeader.setWidth(flagElement.getAttribute("width").trim());
				dhtmlxGridHeader.setType(flagElement.getAttribute("type").trim());
				dhtmlxGridHeader.setAlign(flagElement.getAttribute("align").trim());
				dhtmlxGridHeader.setSort(flagElement.getAttribute("sort").trim());
				dhtmlxGridHeader.setColor(flagElement.getAttribute("color").trim());
				dhtmlxGridHeader.setFormat(flagElement.getAttribute("format").trim());
				dhtmlxGridHeader.setHidden(flagElement.getAttribute("hidden").trim());
				dhtmlxGridHeader.setAttribute(getPrex_() + flagElement.getAttribute("attribute").trim());
				dhtmlxGridHeader.setAuth(flagElement.getAttribute("auth").trim());
				dhtmlxGridHeader.setFooter(flagElement.getAttribute("footer").trim());
				dhtmlxGridHeader.setStat(flagElement.getAttribute("stat").trim());
				dhtmlxGridHeader.setExcelStyle(flagElement.getAttribute("excelStyle").trim());
				items.add(dhtmlxGridHeader);
			}
		}
	}
}
